import pandas as pd


class InsuranceMetricsCalculator:
    """保险业务指标计算器，实现所有指标的核心计算"""

    def __init__(self, data: pd.DataFrame):
        self.data = data  # 输入数据（从数据源层获取）

    def get_original_premium_growth_rate(self, report_period: str, base_period: str) -> float:
        """计算原保费收入增长率"""
        report_val = self.data.loc[self.data["period"] == report_period, "original_premium"].values[0]
        base_val = self.data.loc[self.data["period"] == base_period, "original_premium"].values[0]
        return (report_val - base_val) / base_val * 100

    def get_retained_premium_growth_rate(self, report_period: str, base_period: str) -> float:
        """计算自留保费增长率"""
        report_val = self.data.loc[self.data["period"] == report_period, "retained_premium"].values[0]
        base_val = self.data.loc[self.data["period"] == base_period, "retained_premium"].values[0]
        return (report_val - base_val) / base_val * 100

    def get_original_market_share(self, period: str) -> float:
        """计算原保费收入市场份额"""
        company_val = self.data.loc[self.data["period"] == period, "original_premium"].values[0]
        market_val = self.data.loc[self.data["period"] == period, "market_original_total"].values[0]
        return company_val / market_val * 100

    def get_incremental_market_share(self, report_period: str, base_period: str) -> float:
        """计算原保费收入增量市场份额"""
        # 公司增量
        company_report = self.data.loc[self.data["period"] == report_period, "original_premium"].values[0]
        company_base = self.data.loc[self.data["period"] == base_period, "original_premium"].values[0]
        company_increment = company_report - company_base

        # 市场增量
        market_report = self.data.loc[self.data["period"] == report_period, "market_original_total"].values[0]
        market_base = self.data.loc[self.data["period"] == base_period, "market_original_total"].values[0]
        market_increment = market_report - market_base

        if market_increment == 0:
            return 0.0  # 避免除零错误
        return company_increment / market_increment * 100

    def get_development_coefficient(self, report_period: str, base_period: str) -> float:
        """计算发展系数"""
        incremental_share = self.get_incremental_market_share(report_period, base_period)
        market_share = self.get_original_market_share(report_period)
        if market_share == 0:
            return 0.0  # 避免除零错误
        return incremental_share / market_share

    def get_first_year_premium_ratio(self, period: str) -> float:
        """计算首年保费与保费收入比"""
        first_year = self.data.loc[self.data["period"] == period, "first_year_premium"].values[0]
        total = self.data.loc[self.data["period"] == period, "original_premium"].values[0]
        return first_year / total * 100

    def get_13_month_renewal_rate(self, period: str) -> float:
        """计算13个月续保率"""
        month13 = self.data.loc[self.data["period"] == period, "policy_count_month13"].values[0]
        month1 = self.data.loc[self.data["period"] == period, "policy_count_month1"].values[0]
        return month13 / month1 * 100

    def get_agent_average_premium(self, period: str) -> float:
        """计算人均保费（个人营销渠道）"""
        std_premium = self.data.loc[self.data["period"] == period, "standard_premium"].values[0]
        agent_begin = self.data.loc[self.data["period"] == period, "agent_count_begin"].values[0]
        agent_end = self.data.loc[self.data["period"] == period, "agent_count_end"].values[0]
        avg_agents = (agent_begin + agent_end) / 2
        return std_premium / avg_agents

    def get_standard_premium_growth_rate(self, report_period: str, base_period: str) -> float:
        """计算标准保费增长率"""
        report_val = self.data.loc[self.data["period"] == report_period, "standard_premium"].values[0]
        base_val = self.data.loc[self.data["period"] == base_period, "standard_premium"].values[0]
        return (report_val - base_val) / base_val * 100

    def get_retained_premium_to_equity_ratio(self, period: str) -> float:
        """计算自留保费占净资产比"""
        retained = self.data.loc[self.data["period"] == period, "retained_premium"].values[0]
        equity_begin = self.data.loc[self.data["period"] == period, "owner_equity_begin"].values[0]
        equity_end = self.data.loc[self.data["period"] == period, "owner_equity_end"].values[0]
        avg_equity = (equity_begin + equity_end) / 2
        return retained / avg_equity * 100